package dst.ass1.jpa.model.impl;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;

import dst.ass1.jpa.model.IMembershipKey;
import dst.ass1.jpa.model.IUser;
import dst.ass1.jpa.model.IWorkPlatform;

@Embeddable
public class MembershipKey implements IMembershipKey, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManyToOne(targetEntity = User.class,cascade = { CascadeType.PERSIST, CascadeType.MERGE },fetch=FetchType.EAGER)  //to-one properties are loaded eagerly (as loading them is cheap))
	private IUser user;
	
	@ManyToOne(targetEntity = WorkPlatform.class,cascade = { CascadeType.PERSIST, CascadeType.MERGE },fetch=FetchType.EAGER)  //to-one properties are loaded eagerly (as loading them is cheap))
	private IWorkPlatform workPlatform;

	public MembershipKey() {
	}

	@Override
	public IUser getUser() {
		return this.user;
	}

	@Override
	public void setUser(IUser user) {
		this.user = (User) user;
	}

	@Override
	public IWorkPlatform getWorkPlatform() {
		return this.workPlatform;
	}

	@Override
	public void setWorkPlatform(IWorkPlatform platform) {
		this.workPlatform = (WorkPlatform) platform;
	}

	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}

		if (!(that instanceof MembershipKey)) {
			return false;
		}

		MembershipKey thatKey = (MembershipKey) that;

		boolean isUserEqual = (user == null) ? thatKey.user == null : user
				.equals(thatKey.user);
		boolean isGridEqual = (workPlatform == null) ? thatKey.workPlatform == null
				: workPlatform.equals(thatKey.workPlatform);

		return (isUserEqual && isGridEqual);
	}

	@Override
	public int hashCode() {
		if (user == null) {
			return 0;
		}

		int hashCode = user.hashCode();

		if (this.workPlatform != null) {
			hashCode ^= workPlatform.hashCode();
		}

		return hashCode;
	}

}
